//
//  WZOutCountController.m
//  ETong
//
//  Created by wanzhao on 15/11/24.
//  Copyright (c) 2015年 wanzhao. All rights reserved.
//

#import "WZOutCountController.h"
#import "WZOutCountFrame.h"
#import "WZOutCountCell.h"
#import "DatePickerView.h"
#import "NSDate+Extension.h"
#import "CFolderMgr.h"
#import "MJExtension.h"
#import "WZOutCountOrderView.h"
#import "WZLocationViewController.h"
#import "MBProgressHUD+WZ.h"
#import "NetWorkShare.h"

@interface WZOutCountController ()<UITableViewDataSource, UITableViewDelegate, DatePickerViewDelegate, WZOutCountOrderViewDelegate>

@property (nonatomic, strong) CAccountModel *curAccount;
@property (nonatomic, strong) NSMutableArray *outCountDataArray;
@property (nonatomic, weak) UITableView *tableView;
@property (nonatomic, weak) UILabel *timeLabel;
@property (nonatomic, weak) UILabel *dateLabel;
@property (nonatomic, weak) WZOutCountOrderView *orderView;

@property (nonatomic, weak) DatePickerView *datePickerView;

@property (nonatomic, copy) NSString *orderStr;
@property (nonatomic, copy) NSString *dateStr;
@property (nonatomic, strong) NSDateFormatter *fm;


@end

@implementation WZOutCountController


- (void)dealloc
{
    NSLog(@"WZOutCountController  dealloc");
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = WhiteColor;
    self.navigationItem.title = @"外出签到统计";
    
    if ([self.navigationController.navigationBar respondsToSelector:@selector( setBackgroundImage:forBarMetrics:)]){
        NSArray *list=self.navigationController.navigationBar.subviews;
        for (UIView *sub in list) {
            for (UIView *sub1 in sub.subviews) {
                if (sub1.height == 0.5f) {
                    sub1.hidden = YES;
                }
            }
        }
    }
    
    self.curAccount = [[CFolderMgr shareInstance].accMgr getRecentlyLoginAccount];
    self.orderStr = @"1";
    
    _fm = [[NSDateFormatter alloc] init];
    _fm.timeZone = [NSTimeZone systemTimeZone];
    _fm.locale = [NSLocale currentLocale];
    _fm.dateFormat = @"yyyy-MM-dd";
    self.dateStr = [_fm stringFromDate:[NSDate date]];

    
    //创建顶部排序选择控件
    UIView *dateView = [[UIView alloc] init];
    dateView.backgroundColor = RGBCOLORWITH16COLOR(0x3899e8);
    dateView.frame = CGRectMake(0, 0, self.view.width, 44.0f);
    [self.view addSubview:dateView];
    
    //创建时间选择按钮
    UIButton *timeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    timeBtn.tag = 30002;
    timeBtn.backgroundColor = WhiteColor;
    timeBtn.frame = CGRectMake(self.view.width - OppositeWidthRate(39), OppositeHeightRate(10), 24.0f, 24.0f);
    [timeBtn setImage:[UIImage imageNamed:@"outDownArrow"] forState:UIControlStateNormal];
    [timeBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [dateView addSubview:timeBtn];
    
    //创建时间显示label
    UILabel *timeLabel = [[UILabel alloc] init];
    timeLabel.backgroundColor = WhiteColor;
    timeLabel.text = @"时间";
    timeLabel.frame = CGRectMake(timeBtn.x - OppositeWidthRate(60), OppositeHeightRate(10), OppositeWidthRate(60), 24.0f);
    timeLabel.textColor = RGBCOLORWITH16COLOR(0x333333);
    timeLabel.font = Font14;
    timeLabel.textAlignment = UITextAlignmentCenter;
    [dateView addSubview:timeLabel];
    self.timeLabel = timeLabel;
    
    //创建日历按钮
    UIButton *dateBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    dateBtn.tag = 30001;
    dateBtn.backgroundColor = WhiteColor;
    dateBtn.frame = CGRectMake(timeLabel.x - OppositeWidthRate(10) - 24.0f, OppositeHeightRate(10), 24.0f, 24.0f);
    [dateBtn setImage:[UIImage imageNamed:@"Outcalender"] forState:UIControlStateNormal];
    [dateBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [dateView addSubview:dateBtn];

    //创建日期显示label
    UILabel *dateLabel = [[UILabel alloc] init];
    dateLabel.backgroundColor = WhiteColor;
    dateLabel.text = [NSDate dateStringForDate:[NSDate date]];
    dateLabel.frame = CGRectMake(OppositeWidthRate(15), OppositeHeightRate(10), dateBtn.x - OppositeWidthRate(15), 24.0f);
    dateLabel.textColor = RGBCOLORWITH16COLOR(0x333333);
    dateLabel.font = Font14;
    dateLabel.textAlignment = UITextAlignmentCenter;
    [dateView addSubview:dateLabel];
    self.dateLabel = dateLabel;
    
    //创建数据空间tableView
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(dateView.frame), self.view.width, HARDWARE_SCREEN_HEIGHT - 64 - CGRectGetMaxY(dateView.frame)) style:UITableViewStyleGrouped];
    tableView.showsVerticalScrollIndicator = NO;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.backgroundColor = ClearColor;
    tableView.delegate = self;
    tableView.dataSource = self;
    [self.view addSubview:tableView];
    self.tableView = tableView;
    
    [self loadOutData];
}

- (void)loadOutData
{
    [MBProgressHUD showMessage:nil toView:self.view];
    
    [NetWorkShare getOutSignListWithUserId:self.oaUserId staticDate:self.dateStr orderFlag:self.orderStr successHandle:^(AFHTTPRequestOperation *operation, id responseObject) {
         [MBProgressHUD hideHUDForView:self.view];
        if (responseObject) {
            
            if ([[responseObject objectForKey:@"success"] boolValue]) {
                [self.outCountDataArray removeAllObjects];
                NSArray *outArray = [WZOut objectArrayWithKeyValuesArray:responseObject[@"list"]];
                for (WZOut *wzout in outArray) {
                    WZOutCountFrame *outCountFrame = [[WZOutCountFrame alloc] init];
                    outCountFrame.wzOut = wzout;
                    [self.outCountDataArray addObject:outCountFrame];
                }
                
            } else {
                [MBProgressHUD showError:[responseObject objectForKey:@"message"]];
            }
            
        } else {
            [MBProgressHUD showError:@"未知错误"];
        }
        [self.tableView reloadData];
    } failureHandle:^(AFHTTPRequestOperation *operation, NSError *error) {
        [MBProgressHUD hideHUDForView:self.view];
        [self.tableView reloadData];

    }];
    
}


- (void)addTestData
{
    
//    WZOut *wzout = [[WZOut alloc] init];
//    wzout.companyName = @"万朝科技有限公司";
//    wzout.outApplyTime = @"2015-11-24 18:00";
//    wzout.outAddress = @"北京市朝阳区建国路甲2号";
//    wzout.outActualAddress = @"北京市朝阳区建国路甲2号";
//    wzout.outType = @"1";
//    WZOutCountFrame *outFrame = [[WZOutCountFrame alloc] init];
//    outFrame.wzOut = wzout;
//    [self.outCountDataArray addObject:outFrame];
    
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if (!IsIOS7) {
        self.navigationController.navigationBar.tintColor = RGBCOLORWITH16COLOR(0x3899e8);
    } else {
        self.navigationController.navigationBar.barTintColor = RGBCOLORWITH16COLOR(0x3899e8);
    }
    
}

- (void)btnClick:(UIButton *)btn
{
    switch (btn.tag) {
            //排序选择
        case 30001:
        {
            [self.orderView removeFromSuperview];
            self.orderView.outOrderdelegate = nil;
            self.orderView = nil;
            
            if (self.datePickerView) {
                [self.datePickerView removeFromSuperview];
                self.datePickerView.delegate = nil;
                self.datePickerView = nil;
            } else {
                DatePickerView *picker = [[DatePickerView alloc] initWithWidth:HARDWARE_SCREEN_WIDTH Height:226.0f TimeType:dateDetail WithTitle:nil];
                picker.delegate =  self;
                picker.frame = CGRectMake(0, self.tableView.y, HARDWARE_SCREEN_WIDTH, 226.0f);
                [self.view addSubview:picker];
                self.datePickerView = picker;
            }
        }
            break;
            //日期选择
        case 30002:
        {
            [self.datePickerView removeFromSuperview];
            self.datePickerView.delegate = nil;
            self.datePickerView = nil;
            if (self.orderView) {
                [self.orderView removeFromSuperview];
                self.orderView.outOrderdelegate = nil;
                self.orderView = nil;
            } else {
                WZOutCountOrderView *orderView = [[WZOutCountOrderView alloc] init];
                orderView.outOrderdelegate =  self;
                orderView.frame = CGRectMake(0, self.tableView.y, HARDWARE_SCREEN_WIDTH, 88.0f);
                [self.view addSubview:orderView];
                self.orderView = orderView;

            }
           
        }
            break;
            
        default:
            break;
    }
}

#pragma mark pickview代理方法
- (void)datePickerView:(DatePickerView *)pickerView selectDate:(NSDate *)result
{
    
    self.dateLabel.text = [NSDate dateStringForDate:result];
    self.dateStr = [_fm stringFromDate:result];
    
    [self loadOutData];
}


#pragma mark orderview代理方法
- (void)OutCountOrderViewDidSelectCellWithIndexPath:(NSIndexPath *)indexPath orderText:(NSString *)orderText
{
    [self.orderView removeFromSuperview];
    self.orderView.outOrderdelegate = nil;
    self.orderView = nil;
    if (indexPath.row == 0) {
        self.orderStr = @"1";
        self.timeLabel.text = orderText;
    } else {
        self.orderStr = @"2";
        self.timeLabel.text = orderText;
    }
    [self loadOutData];
}
#pragma mark 懒加载
- (NSMutableArray *)outCountDataArray
{
    if (_outCountDataArray == nil) {
        _outCountDataArray = [NSMutableArray array];
    }
    return _outCountDataArray;
}

#pragma mark tableView相关方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.outCountDataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WZOutCountFrame *outFrame = self.outCountDataArray[indexPath.row];
    return outFrame.cellHeight;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01f;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = ClearColor;
    return view;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = ClearColor;
    return view;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WZOutCountCell *cell = [WZOutCountCell outCountCellWithTableView:tableView];
    WZOutCountFrame *outFrame = self.outCountDataArray[indexPath.row];
    cell.outCountFrame = outFrame;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}


@end
